//
//  DLPhotoCollectionCell.swift
//  图片选择器
//
//  Created by danli on 15/12/30.
//  Copyright © 2015年 danli. All rights reserved.
//

import UIKit

//@objc protocol DLPhotoCollectionCellDelegate:NSObjectProtocol
//{
////    optional  加这个可实现可不实现
//    // 要加可选的需要在协议前面添加 @objc
//    optional func photoCollectionCellAddBtnDidClick();
//    func photoCollectionCellRemoveBtnDidClick();
//}

@objc protocol DLPhotoCollectionCellDelegate:NSObjectProtocol
{
    //    optional  加这个可实现可不实现
    func photoCollectionCellAddBtnDidClick(cell:DLPhotoCollectionCell);
    func photoCollectionCellRemoveBtnDidClick(cell:DLPhotoCollectionCell);
}


class DLPhotoCollectionCell: UICollectionViewCell {
    
    /// 代理
    weak var delegate: DLPhotoCollectionCellDelegate?
    
    var image:UIImage?{
        didSet{
            addBtn.setImage(image, forState: UIControlState.Normal)
            removeBtn.hidden = false
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func prepareUI()
    {
        contentView.addSubview(addBtn)
        contentView.addSubview(removeBtn)
        
        addBtn.translatesAutoresizingMaskIntoConstraints = false
        removeBtn.translatesAutoresizingMaskIntoConstraints = false
        
     contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[addBtn]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["addBtn":addBtn]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[addBtn]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["addBtn":addBtn]))
        
        // 水平方向和父控件右边重合，顶部和父控件顶部重合
      contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[removeBtn]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["removeBtn":removeBtn]))
          contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[removeBtn]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["removeBtn":removeBtn]))
        
        
    }
    
    /// 添加添加按钮
    func addAddBtn()
    {
        addBtn.setImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
        removeBtn.hidden = true
    }
    
    // MARK: 按钮点击方法
    
    /// 加号按钮点击事件
    func addBtnDidClick()
    {
        // 如果代理是可选的，需要进行拆包或者 加问号， 加"?"比较安全
//        delegate?.photoCollectionCellRemoveBtnDidClick?()
        delegate?.photoCollectionCellAddBtnDidClick(self)
    }
    
    // 删除按钮点击事件
    func removeBtnDidClick()
    {
        delegate?.photoCollectionCellRemoveBtnDidClick(self)
    }
    
    // MARK:懒加载
    lazy var addBtn:UIButton = {
       
        let btn = UIButton()
        btn.setImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
        
        // 设置图片的填充模式
        btn.imageView?.contentMode = UIViewContentMode.ScaleAspectFill
        
        btn.addTarget(self, action: "addBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        
    }()
    
    lazy var removeBtn:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "removeBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
//        btn.hidden = true
        return btn
    }()
    
}
